import { getDictItemsFromCache } from '@/api/api'
import Api from '@/api/sjsk/performanceServiceQuery'

const searchExamine = {
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'assessSort',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '计划编号',
          align: "center",
          dataIndex: 'planNo'
        },
        {
          title: '计划名称',
          align: "center",
          dataIndex: 'planName',
        },
        {
          title: '考核类型',
          align: "center",
          dataIndex: 'assessType',
          customRender: (text, record, index) =>
            `${this.setTittle(record)}`
        },
        {
          title: '考核周期',
          align: "center",
          dataIndex: 'assessCycle'
        },
        {
          title: '考核时间',
          align: "center",
          dataIndex: 'assessTime'
        },
        {
          title: '下发单位',
          align: 'center',
          dataIndex: 'issuedUnit'
        },
        {
          title: '考核对象',
          align: "center",
          dataIndex: 'assessUser'
        },
        {
          title: '考核得分',
          align: 'center',
          dataIndex: 'assessScore'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      // 考核指标表头
      columnsBgt: [
        {
          title: '序号',
          dataIndex: 'assessSort',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '计划编号',
          align: "center",
          dataIndex: 'planNo'
        },
        {
          title: '计划名称',
          align: "center",
          dataIndex: 'planName',
        },
        {
          title: '考核类型',
          align: "center",
          dataIndex: 'assessType',
          customRender: (text, record, index) =>
            `${this.setTittle(record)}`
        },
        {
          title: '考核时间',
          align: "center",
          dataIndex: 'assessTime'
        },
        {
          title: '考核周期',
          align: 'center',
          dataIndex: 'assessCycle'
        },
        {
          title: '下发单位',
          align: "center",
          dataIndex: 'issuedUnit'
        },
        {
          title: '考核对象',
          align: "center",
          dataIndex: 'assessUser'
        },
        {
          title: '指标名称',
          align: 'center',
          dataIndex: 'indicatorName'
        },
        {
          title: '考核标准',
          align: 'center',
          dataIndex: 'standardAssessment'
        },
        {
          title: '基础分值',
          align: 'center',
          dataIndex: 'firstScore'
        },
        {
          title: '任务量',
          align: 'center',
          dataIndex: 'taskNumber'
        },
        {
          title: '基数',
          align: 'center',
          dataIndex: 'cardinalNumber'
        },
        {
          title: '权重（%）',
          align: 'center',
          dataIndex: 'weight'
        },
        {
          title: '指标类型',
          align: 'center',
          dataIndex: 'indicatorType'
        },
        {
          title: '上限分值',
          align: 'center',
          dataIndex: 'lowerScore'
        },
        {
          title: '下限分值',
          align: 'center',
          dataIndex: 'upperScore'
        },
        {
          title: '考核得分',
          align: 'center',
          dataIndex: 'assessScore'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      examinType: getDictItemsFromCache('examinType'),
      examinStatus: getDictItemsFromCache('examinStatus'),
      loading: false,
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam:{},
      assessTime:'',
      assessTimeObject:{
        assessTimeStart:'',
        assessTimeEnd:'',
      },
      pageData: {},
      componentName:'' // myExamine,examineQuery,bgtQuery
    }
  },
  created(){
    this.componentName = this.$route.meta.componentName;
    if(this.componentName === 'bgtQuery'){
      this.columns = this.columnsBgt;
    }
    this.onQuery()
  },
  methods: {
    setTittle(val) {
      return this.$filterDictTextByCache('examinType', val.assessType) //现在字典中对应的文字，考核类型
    },
    // 查询
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    // 重置
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10
      }
      this.refreshTable()
    },
    onSelectedRow(keys) {

    },
    onTableChange(pagination, pageFilter, sorter) {
       let keyMap = { currentPage: 'pageNo' }
       let paginations = Object.keys(pagination).reduce((newData, key) => {
         let newKey = keyMap[key] || key
         newData[newKey] = pagination[key]
         return newData
       }, {})
       this.pageFilter = Object.assign({}, this.pageFilter, paginations)
       this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      if(this.assessTime){
        this.queryParam = Object.assign({}, this.queryParam, this.assessTimeObject)
      }
      let params = Object.assign({}, this.queryParam, this.pageFilter)
      let interfaceMethod = null;
      if(this.componentName === 'examineQuery'){
        interfaceMethod = Api.getAssess(params)
      }else if(this.componentName === 'bgtQuery'){
        interfaceMethod = Api.getAssessIndex(params)
      }else {
        interfaceMethod = Api.getMyAssessList(params)
      }
      interfaceMethod.then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          this.loading = false
          this.$refs.table.onClearRowKeys()
        } else {
          this.loading = false
          this.pageData = {}
          this.$message.warn('请求失败！')
        }
      }).catch((e) => {
        this.loading = false
        this.$message.warn('请求失败！')
      })
    },
    onDetail(record) {
      if(this.componentName === 'bgtQuery'){
        this.visible = true;
      }else{
        this.$message.warn('待需求确认！')
        /*this.$router.push({
          path: '/searchExamine/createReport',
        })*/
      }

    },
    onChangeCheck(dates, field) {
      if(Array.isArray(dates)) { //选择时间范围执行
        this.assessTimeObject.assessTimeStart = dates[0]
        this.assessTimeObject.assessTimeEnd = dates[1]
      }
    },
  }
}
export default searchExamine